package com.hjb.toolbardemo.base;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.hjb.toolbardemo.R;
import com.hjb.toolbardemo.application.MyApplication;
import com.hjb.toolbardemo.utils.PopupWindowUtils;
import com.readystatesoftware.systembartint.SystemBarTintManager;


/**
 * 作者 : hjb
 * 时间 : 2016/4/25.
 */
public class BaseActivity extends AppCompatActivity {

    private PopupWindowUtils popupWindowUtils;

    public void setTitleTv(int id) {
        TextView tv = (TextView) findViewById(R.id.title_tv);
        tv.setText(id);
    }

    public void setTitleTv(String str) {
        TextView tv = (TextView) findViewById(R.id.title_tv);
        tv.setText(str);
    }

    public void setNextTv(String str) {
        TextView tv = (TextView) findViewById(R.id.next_tv);
        tv.setVisibility(View.VISIBLE);
        tv.setText(str);
        View iv = findViewById(R.id.next_img);
        iv.setVisibility(View.GONE);
    }

    public TextView getNextTv() {

        return (TextView) findViewById(R.id.next_tv);
    }

    public void setBackGone() {
        View tv = findViewById(R.id.ly_back);
        tv.setVisibility(View.GONE);
    }

    public void back(View view) {
        finish();
    }

    @SuppressLint("NewApi")
    public void setNextTvBG(Drawable drawable) {
        TextView tv = (TextView) findViewById(R.id.next_tv);
        tv.setBackground(drawable);
    }

    public void setNextImage(int id) {
        TextView tv = (TextView) findViewById(R.id.next_tv);
        tv.setVisibility(View.GONE);
        ImageView iv = (ImageView) findViewById(R.id.next_img);
        iv.setVisibility(View.VISIBLE);
        iv.setImageResource(id);
    }

    public void setNextOnClick(View.OnClickListener onClick) {
        TextView tv = (TextView) findViewById(R.id.next_tv);
        tv.setOnClickListener(onClick);
        View iv = findViewById(R.id.next_img);
        iv.setOnClickListener(onClick);
    }

    public void setNextGone() {
        TextView tv = (TextView) findViewById(R.id.next_tv);
        tv.setVisibility(View.GONE);
        ImageView iv = (ImageView) findViewById(R.id.next_img);
        iv.setVisibility(View.GONE);
    }

    @Override
    protected void onCreate(Bundle savedInstanced) {
        super.onCreate(savedInstanced);
        // 竖屏锁定
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setSystemBarTint();
        ((MyApplication) this.getApplication()).addActivity(this);
    }

    /**
     * 设置沉浸式
     */
    private void setSystemBarTint() {
        //只对api19以上版本有效
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }
        //为状态栏着色
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(R.color.head_color);
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (popupWindowUtils != null) {
                dismissProgress();
                return true;
            }
        }
        return false;
    }

    public void showProgress(Activity activity, View view) {
        showProgress(activity, view, null);
    }

    public void showProgress(Activity activity, View view, String message) {
        popupWindowUtils = new PopupWindowUtils(activity, R.layout.dialog_wait,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, view);
        View windView = popupWindowUtils.showCenter();
        if (message != null) {
            TextView textView = (TextView) windView.findViewById(R.id.tv);
            textView.setText(message);
        }
    }

    public void dismissProgress() {
        if (popupWindowUtils != null) {
            popupWindowUtils.dismissPopuWindow();
            popupWindowUtils = null;
        }
    }
}